package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lv.common.imp.OnPositionTListener
import com.lv.common.utils.showToast
import com.lv.service.R
import com.lv.service.adapter.TextTabSelectedMoreAdapter
import com.lv.common.bean.CommonSelectedBean
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：底部公共list选择器 tab 类型 多选
 */
class RecyclerSelectedTabPopupView(
    private val mContext: Context,
    private val mAllBeans: List<CommonSelectedBean>? = null,
    private val mTitle: String,
    private val topH: Int = 0, private val mSelectedMore: Boolean = true,//是否多选
    private val mPositionTListener: OnPositionTListener<List<CommonSelectedBean>>? = null,
//    private var current: Int = -1,//默认没有选中
) :
    BottomPopupView(mContext) {

    private var rvContent: RecyclerView? = null
    private var tvTitle: TextView? = null
    private var viewTop: View? = null
    private var viewShadow: TextView? = null
    private val mBeans = mutableListOf<CommonSelectedBean>()

    private var mAdapter: TextTabSelectedMoreAdapter? = null
    override fun getImplLayoutId(): Int {
        return R.layout.recycler_selected_tag_more_popup
    }

    override fun onCreate() {
        super.onCreate()
        rvContent = findViewById<RecyclerView>(R.id.rv_content)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        viewTop = findViewById<View>(R.id.view_top)
        viewShadow = findViewById<TextView>(R.id.tv_submit)
        findViewById<ImageView>(R.id.iv_close).setOnClickListener { dismiss() }
        val layoutParams = viewTop?.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.height = topH
        initView()
        initEvnet()
    }

    private fun initView() {
        mAllBeans?.let {
            mBeans.clear()
            mAllBeans.forEach { commonSelectedBean ->
                mBeans.add(
                    CommonSelectedBean(
                        commonSelectedBean.name,
                        commonSelectedBean.tag,
                        commonSelectedBean.selected
                    )
                )
            }
        }
        tvTitle?.text = mTitle

        val mLinearLayoutManager = GridLayoutManager(context, 3)
        rvContent?.layoutManager = mLinearLayoutManager
        mAdapter = TextTabSelectedMoreAdapter(mBeans)
        rvContent?.adapter = mAdapter
        mAdapter?.onViewClickListener = OnPositionTListener { position, data ->
            if (mSelectedMore) {
                mBeans[position].selected = !mBeans[position].selected
                mAdapter?.notifyItemChanged(position)
            } else {
                var current = mBeans.indexOfFirst { it.selected }
                if (current == position) {
                    return@OnPositionTListener
                }
                if (current != -1) {
                    mBeans[current].selected = false
                    mAdapter?.notifyItemChanged(current)
                }
                current = position
                mBeans[current].selected = true
                mAdapter?.notifyItemChanged(current)
            }

//            dismiss()
        }

    }

    private fun initEvnet() {

        viewTop?.setOnClickListener {

            dismiss()
        }

        viewShadow?.setOnClickListener {
//            mPositionTListener?.onClick(current, mBeans[current])
            var current = -1
            for (i in 0 until mBeans.size) {
                val commonSelectedBean = mBeans[i]
                if (commonSelectedBean.selected) {
                    current = i
                    break
                }
            }

            if (current == -1) {
                showToast("请至少选择一项")
                return@setOnClickListener
            }
            mPositionTListener?.onClick(0, mBeans)
            if (mEnableClosed) {
                dismiss()
            }
        }
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }

    fun setSubText(subText: String) {
        viewShadow?.text = subText
    }

    private var mEnableClosed = true
    fun setCloseEnAble(enable: Boolean) {
        mEnableClosed = enable
    }

    fun setArea(mFZFWAreaBeans: MutableList<CommonSelectedBean>) {
        mFZFWAreaBeans?.let {
            mBeans.clear()
            mFZFWAreaBeans.forEach { commonSelectedBean ->
                mBeans.add(
                    CommonSelectedBean(
                        commonSelectedBean.name,
                        commonSelectedBean.tag,
                        commonSelectedBean.selected
                    )
                )
            }
        }
        mAdapter?.notifyDataSetChanged()
    }

}